<!DOCTYPE html>
<html lang="en">
<head>
  <title></title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="css/style.css" rel="stylesheet">
  <link href="./style.css" rel="stylesheet">
</head>
<body>
  <div id="root"></div>
  
  <script src="../lib/react.js"></script>
  <script src="../lib/react-dom.js"></script>
  <script src="../lib/babel-min.js"></script>
  <script src="./data.js"></script>

  <script type="text/babel">
    class App extends React.Component {
      constructor() {
        super();
        this.state = {
          books: books
        }
      }

      getTotalPrice() {
        const totalPrice = this.state.books.reduce((preValue, item) => {
          return preValue + item.count * item.price;
        }, 0)
        return totalPrice;
      }

      changeCount(index, count) {
        const newBooks = [...this.state.books]
        newBooks[index].count += count;
        this.setState({
          books: newBooks
        })
      }

      remove(index) {
        const newBooks = [...this.state.books];
        newBooks.splice(index, 1)
        this.setState({
          books: newBooks
        })
      }

      renderBooks() {
        const { books } = this.state;
        return (
          <div>
              <table>
                <thead>
                  <tr>
                    <th>序号</th>
                    <th>书籍名称</th>
                    <th>出版日期</th>
                    <th>价格</th>
                    <th>购买数量</th>
                    <th>操作</th>
                  </tr>
                </thead>
                <tbody>
                  {
                    books.map((item, index) => {
                      return (
                        <tr key={item.id}>
                          <td>{index + 1}</td>
                          <td>{item.name}</td>
                          <td>{item.data}</td>
                          <td>{"￥" + item.price}</td>
                          <td>
                            <button onClick={() => this.changeCount(index, -1)} disabled={item.count <= 1}>-</button>
                            {item.count}
                            <button onClick={() => this.changeCount(index, 1)}>+</button>
                          </td>
                          <td>
                            <button onClick={() => this.remove(index)}>删除</button>
                          </td>
                        </tr>
                      )
                    })
                  }
                </tbody>
              </table>
              <h2>总价格：{"￥" + this.getTotalPrice()}</h2>
            </div>
        )

      }
      renderEmpty() {
        return (
          <div>
            <h2>购物车为空...</h2>
          </div>
        )
      }

      render() {
        const { books } = this.state;
        return books.length ? this.renderBooks() : this.renderEmpty()
      }
    }
    
    const root = ReactDOM.createRoot(document.querySelector("#root"))
    root.render(<App/>)

  </script>

</body>
</html>